<?php

/**
 * checkmail actions.
 *
 * @package    sf_sandbox
 * @subpackage checkmail
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class checkmailActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {

  	    ////Удаляем
  	    if($request->getParameter('del')==1)
  	    {
  	        $m = MailboxPeer::retrieveByPK($request->getParameter('id'));
		  	if($this->getUser()->hasCredential('admin') and $m)
		  	{
		  		$m->delete();
		  	}
  	    }
  	
  	    $c = new Criteria();
  	    
  	    
  	     /// Делаем критерий

          if($request->getParameter('mailbox_filters[alert]'))
        {
        	$regex = MailboxPeer :: BODY." REGEXP '[[:digit:]]'";
            $c->add(MailboxPeer :: BODY, $regex, Criteria::CUSTOM);
        	
            //$cq = "substring( ". MailboxPeer :: BODY.", 1, 1 ) NOT BETWEEN  'a' AND  'z'";
            //$c->add( MailboxPeer :: BODY, $cq, Criteria::CUSTOM);
            
            
        	//$c->addAnd(MailboxPeer::FROM_ID,$request->getParameter('mailbox_filters[from_id]'));
        	
        }
  	    
        if($request->getParameter('mailbox_filters[from_id]'))
        {
        	$c->addAnd(MailboxPeer::FROM_ID,$request->getParameter('mailbox_filters[from_id]'));
        	
        }

          if($request->getParameter('mailbox_filters[to_id]'))
        {
        	$c->addAnd(MailboxPeer::TO_ID,$request->getParameter('mailbox_filters[to_id]'));
        	
        }
        
          if($request->getParameter('mailbox_filters[date_from]'))
        {
        	$c->addAnd(MailboxPeer::CREATED_AT,date('Y-m-d', strtotime($request->getParameter('mailbox_filters[date_from]'))),Criteria::GREATER_EQUAL);
            
        }
        
            if($request->getParameter('mailbox_filters[date_to]'))
        {
        	$c->addAnd(MailboxPeer::CREATED_AT,date('Y-m-d', strtotime($request->getParameter('mailbox_filters[date_to]'))),Criteria::LESS_EQUAL);
        	
        }        
        
          if(!$request->getParameter('page')){
  	    	$this->page = 1;
  	    }
  	    else {
  	    	$this->page=$request->getParameter('page');
  	    } 
  	    
      	        $pager = new sfPropelPager ( 'Mailbox', sfConfig::get ( 'app_payment_perpage' ) );
		$pager->setCriteria ( $c );
		$pager->setPeerMethod ( 'doSelect' );
		$pager->setPage ( $this->page );
		$pager->init ();
		$this->mail = $pager;
  }
  

  
   public function executeEdit(sfWebRequest $request)
  {
  	$m = MailboxPeer::retrieveByPK($request->getParameter('id'));
  	$this->form = new MailboxForm($m);
  	
    if ($request->isMethod('post'))
    {

      $this->form->bind($request->getParameter('mailbox'),$request->getFiles('mailbox'));
      if ($this->form->isValid())
      {
      	
        $this->form->save();
       $this->redirect('checkmail/index'.$_POST['p']);
       
      }
    }
    
  }
  
  

  
}
